﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.Web;
using CrystalDecisions.ReportSource;
using System.Data;
using BOAcademias;
using TOAcademias;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

/*************************************
Autor: Carlos Sánchez
Fecha de Creación: 06/02/2011
Descripción de la Funcionalidad: Formulario que permite visualizar los reportes generados.
**************************************/   

public partial class TunkiFiles_Reportes_VisorReporte : System.Web.UI.Page
{
    BOReporte boReporte = new BOReporte();
    BOPeriodo boPeriodo = new BOPeriodo();
    BOAsistencia boAsistencia = new BOAsistencia();
 
    protected void Page_Load(object sender, EventArgs e)
    {
        CrystalReportSource crsReporte=new CrystalReportSource();
        DataTable dtbReporte=new DataTable();
        String strRutaReporte = String.Empty;
        DataSet dtsReporte = new DataSet();

        if (Session["Reporte"] == null) Session["Reporte"] = "";
        switch (Session["Reporte"].ToString()) {
            case "Historico":
                dtsReporte.Tables.Add(boReporte.reporteHistoricoAcademias());
                if (Session["TipoReporte"].ToString() == "Resumido")
                {
                    strRutaReporte = Server.MapPath("ReporteHistoricoAcademia.rpt");
                }
                else
                {
                    strRutaReporte = Server.MapPath("ReporteHistoricoAcademiaDeta.rpt");
                }
                break;
            case "Requerimiento":
                dtsReporte.Tables.Add(boReporte.reporteRequerimientoxSeccion());
                strRutaReporte = Server.MapPath("ReporteRequerimientoxSeccion.rpt");
                break;
            case "Asistencias":
                dtsReporte.Tables.Add(boReporte.reporteAsistencias());
                if (Session["TipoReporte"].ToString() == "Resumido")
                {
                    strRutaReporte = Server.MapPath("ReporteAsistencias.rpt");
                }
                else
                {
                    strRutaReporte = Server.MapPath("ReporteAsistenciasDeta.rpt");
                }
                break;
            case "ListaAsistencia":
                strRutaReporte = Server.MapPath("../Reportes/ListaAsistencia.rpt");
                break;
        }

        //Obtener el nombre del servidor del archivo DBModuloAcademias.dbml
        //String Servidor = boReporte.ParametrosConexion().ElementAt(0);
        //String BD = boReporte.ParametrosConexion().ElementAt(1);

        crsReporte.ReportDocument.FileName = strRutaReporte;
        if (Session["Reporte"].ToString() != "ListaAsistencia")
        {
            crsReporte.ReportDocument.SetDataSource(dtsReporte);
        }
        //AsignarConexion(RutaReporte,
        //                rep.ReportDocument,
        //                "", "", true,
        //                Servidor,BD);

        crvReporte.ReportSource = crsReporte;
        crvReporte.SelectionFormula = Session["Filtro"].ToString();        
        //crvReporte.DisplayGroupTree = false;
        crvReporte.BestFitPage = false;
        crvReporte.Height = 1150;
        crvReporte.Width = 900;
        crvReporte.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
        crvReporte.RefreshReport();

    }


    #region Asignar Conexion

    public void AsignarConexion(String FileReport, ReportDocument Reporte, String Username, String Password, Boolean SeguridadIntegrada, String Servername, String BD) //Implements IPrintReport.PrintLogon
    {
        Boolean blnIsConnecting = true;
        ReportDocument crNicasoftStandarReport = new ReportDocument();

        Sections crSections;
        ReportObjects crReportObjects;
        SubreportObject crSubreportObject;
        ReportDocument crSubreportDocument;

        Database crDatabase;
        Tables crTables;
        TableLogOnInfo crTableLogOnInfo;
        ConnectionInfo crConnectioninfo;

        while (blnIsConnecting)
        {
            try
            {
                crConnectioninfo = new ConnectionInfo();
                crConnectioninfo.ServerName = Servername;
                crConnectioninfo.DatabaseName = BD;
                //crConnectioninfo.UserID = Username;
                //crConnectioninfo.Password = Password;
                crConnectioninfo.IntegratedSecurity = SeguridadIntegrada;                

                crNicasoftStandarReport.Load(FileReport);

                crDatabase = Reporte.Database;
                crTables = crDatabase.Tables;

                foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
                {
                    crTableLogOnInfo = crTable.LogOnInfo;
                    crTableLogOnInfo.ConnectionInfo = crConnectioninfo;
                    crTable.ApplyLogOnInfo(crTableLogOnInfo);
                }

                crSections = Reporte.ReportDefinition.Sections;

                foreach (Section crSection in crSections)
                {
                    crReportObjects = crSection.ReportObjects;

                    foreach (ReportObject crReportObject in crReportObjects)
                    {
                        if (crReportObject.Kind == ReportObjectKind.SubreportObject)
                        {

                            crSubreportObject = (SubreportObject)crReportObject;

                            crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);

                            crDatabase = crSubreportDocument.Database;
                            crTables = crDatabase.Tables;

                            foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
                            {
                                crConnectioninfo.ServerName = Servername;
                                crConnectioninfo.DatabaseName = BD;
                                //crConnectioninfo.UserID = Username;
                                //crConnectioninfo.Password = Password;
                                crConnectioninfo.IntegratedSecurity = true;

                                crTableLogOnInfo = crTable.LogOnInfo;
                                crTableLogOnInfo.ConnectionInfo = crConnectioninfo;
                                crTable.ApplyLogOnInfo(crTableLogOnInfo);
                            }
                        }
                    }
                }

                crvReporte.ReportSource = Reporte;
                blnIsConnecting = false;

            }
            catch (Exception ex) { Console.WriteLine(ex.Message.ToString());}

        }
    }

    #endregion

}
